<template>
	<view class="phone-bind-page">
		<!-- 顶部导航 -->
		<!-- <view class="header-bag">
			<view class="back" @click="goBack">
				<image src="/static/img/icon-Get-into@3.png" class="back-icon" mode="aspectFit" />
			</view>
			<view class="header-center">
				<text class="header-title">绑定手机号</text>
			</view>
			<view class="header-right"></view>
		</view> -->
		
		<!-- 登录框 -->
		<view class="loginbox">
			<view class="cont">
				<!-- 手机号输入 -->
				<view class="input-item" id="un">
					<image src="/static/img/login/icon-iphone@2x.png" class="input-icon" />
					<input 
						v-model="phoneNumber" 
						type="number" 
						maxlength="11" 
						class="text-input phone-input" 
						placeholder="请输入手机号" 
						@input="onPhoneInput"
					/>
					<image 
						v-if="phoneNumber" 
						src="/static/img/login/icon-close@2x.png" 
						class="close-icon" 
						@click="clearPhone" 
					/>
				</view>
				
				<!-- 验证码输入 -->
				<view class="input-item">
					<image src="/static/img/login/icon-xinxi@2x.png" class="input-icon verify-icon" />
					<input 
						v-model="verifyCode" 
						type="number" 
						maxlength="6" 
						class="text-input" 
						placeholder="请输入短信验证码" 
						@input="onCodeInput"
					/>
					<view 
						class="get-verify-btn" 
						:class="{ disabled: countdown > 0 }" 
						@click="sendVerifyCode"
					>
						{{ countdown > 0 ? `重新发送(${countdown})` : '发送验证码' }}
					</view>
				</view>
			</view>
			
			<!-- 提交按钮 -->
			<view class="submit-btn" @click="submitBind">提交</view>
		</view>
		
		<!-- 温馨提示弹窗 -->
		<view v-if="showSuccessModal" class="success-modal mask-click">
			<view class="modal-content" @click.stop>
				<view class="modal-title">温馨提示</view>
				<view class="modal-text">已绑定手机号{{ phoneNumber }}</view>
				<view class="modal-buttons">
					<view class="btn-cancel" @click="closeModal">取消</view>
					<view class="btn-confirm" @click="goToHome">确定</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { userApi } from '@/api/index.js'

export default {
	data() {
		return {
			phoneNumber: '',
			verifyCode: '',
			countdown: 0,
			timer: null,
			sendingCode: false,
			showSuccessModal: false,
			unionid: ''
		}
	},
	onLoad() {
		// 获取unionid
		this.unionid = uni.getStorageSync('unionid') || ''
	},
	onUnload() {
		// 清除定时器
		if (this.timer) {
			clearInterval(this.timer)
		}
	},
	methods: {
		// 返回上一页
		goBack() {
			uni.navigateBack()
		},
		
		// 手机号输入处理
		onPhoneInput(e) {
			let value = e.detail.value
			if (value.length > 11) {
				value = value.slice(0, 11)
				this.phoneNumber = value
			}
		},
		
		// 验证码输入处理
		onCodeInput(e) {
			let value = e.detail.value
			if (value.length > 6) {
				value = value.slice(0, 6)
				this.verifyCode = value
			}
		},
		
		// 清空手机号
		clearPhone() {
			this.phoneNumber = ''
		},
		
		// 发送验证码
		async sendVerifyCode() {
			if (this.countdown > 0 || this.sendingCode) {
				return
			}
			
			// 验证手机号
			if (!this.phoneNumber) {
				uni.showToast({
					title: '请输入手机号',
					icon: 'none'
				})
				return
			}
			
			if (this.phoneNumber.length !== 11) {
				uni.showToast({
					title: '请输入正确的手机号',
					icon: 'none'
				})
				return
			}
			
			try {
				this.sendingCode = true
				
				// 先检查手机号是否已被使用
				const checkRes = await userApi.checkMobile({
					mobile: this.phoneNumber,
					salt: '1234'
				})
				
				if (checkRes.r === 0 && checkRes.data.is_used === 0) {
					// 发送验证码
					const sendRes = await userApi.sendMobileCode({
						mobile: this.phoneNumber,
						salt: '1234'
					})
					
					if (sendRes.r === 0) {
						this.startCountdown()
						uni.showToast({
							title: '验证码发送成功',
							icon: 'success'
						})
					} else {
						uni.showToast({
							title: sendRes.msg || '发送失败',
							icon: 'none'
						})
					}
				} else {
					uni.showToast({
						title: checkRes.msg || '该手机号已被使用',
						icon: 'none'
					})
				}
			} catch (error) {
				console.error('发送验证码失败:', error)
				uni.showToast({
					title: '网络异常，请稍后重试',
					icon: 'none'
				})
			} finally {
				this.sendingCode = false
			}
		},
		
		// 开始倒计时
		startCountdown() {
			this.countdown = 60
			this.timer = setInterval(() => {
				this.countdown--
				if (this.countdown <= 0) {
					clearInterval(this.timer)
					this.timer = null
				}
			}, 1000)
		},
		
		// 提交绑定
		async submitBind() {
			// 验证手机号
			if (!this.phoneNumber) {
				uni.showToast({
					title: '手机号码不能为空',
					icon: 'none'
				})
				return
			}
			
			if (this.phoneNumber.length !== 11) {
				uni.showToast({
					title: '请输入正确的手机号',
					icon: 'none'
				})
				return
			}
			
			// 验证验证码
			if (!this.verifyCode) {
				uni.showToast({
					title: '验证码不能为空',
					icon: 'none'
				})
				return
			}
			
			if (this.verifyCode.length !== 6) {
				uni.showToast({
					title: '请输入正确的验证码',
					icon: 'none'
				})
				return
			}
			
			try {
				uni.showLoading({
					title: '绑定中...'
				})
				
				const userInfo = uni.getStorageSync('user_info') || {}
				const res = await userApi.userPhoneBind({
					phone: this.phoneNumber,
					code: this.verifyCode,
					unionid: this.unionid
				})
				
				if (res.r === 0) {
					// 更新用户信息
					const newUserInfo = { ...userInfo, ...res.data }
					uni.setStorageSync('user_info', newUserInfo)
					
					// 显示成功弹窗
					this.showSuccessModal = true
				} else {
					uni.showToast({
						title: res.msg || '绑定失败',
						icon: 'none'
					})
				}
			} catch (error) {
				console.error('绑定手机号失败:', error)
				uni.showToast({
					title: '网络异常，请稍后重试',
					icon: 'none'
				})
			} finally {
				uni.hideLoading()
			}
		},
		
		// 关闭弹窗
		closeModal() {
			this.showSuccessModal = false
		},
		
		// 跳转到首页
		goToHome() {
			this.showSuccessModal = false
			uni.reLaunch({
				url: '/pages/index/index'
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.phone-bind-page {
	background-color: #f7f7f7;
	height: 100vh;
	display: flex;
	flex-direction: column;
}

// 顶部导航
.header-bag {
	height: 88rpx;
	background-color: #ff5033;
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 0 30rpx;
	border-bottom: 1rpx solid #E6E6E6;
	position: relative;
	
	.back {
		padding: 10rpx;
		
		.back-icon {
			width: 24rpx;
			height: 48rpx;
		}
	}
	
	.header-center {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		
		.header-title {
			font-size: 38rpx;
			color: white;
		}
	}
	
	.header-right {
		width: 44rpx; // 占位元素，保持标题居中
	}
}

// 登录框
.loginbox {
	width: 670rpx;
	position: relative;
	margin: 200rpx auto 0;
	padding: 94rpx 0;
	box-shadow: 0 0 30rpx 30rpx #e5f9eb;
	border-radius: 20rpx;
	background-color: white;
}

// 内容容器
.cont {
	width: 100%;
}

// 输入项
.input-item {
	position: relative;
	width: 570rpx;
	height: 98rpx;
	border-radius: 4rpx;
	background-color: white;
	border-bottom: 1rpx solid #f2f2f2;
	margin: 0 auto 32rpx auto;
	display: flex;
	align-items: center;
	
	.input-icon {
		position: absolute;
		left: 32rpx;
		width: 34rpx;
		height: 34rpx;
		z-index: 1;
	}
	
	.verify-icon {
		top: 34rpx;
		width: 38rpx;
		height: 38rpx;
	}
	
	.text-input {
		flex: 1;
		height: 100%;
		border: none;
		background-color: transparent;
		padding-left: 84rpx;
		padding-right: 84rpx;
		font-size: 30rpx;
		color: #333;
	}
	
	.phone-input {
		font-size: 30rpx;
	}
	
	.close-icon {
		position: absolute;
		right: 32rpx;
		width: 42rpx;
		height: 42rpx;
		z-index: 1;
	}
	
	.get-verify-btn {
		position: absolute;
		right: 26rpx;
		top: 22rpx;
		padding: 0 14rpx;
		height: 57rpx;
		line-height: 57rpx;
		border: 1rpx solid #37A962;
		color: #ff5033;
		font-size: 28rpx;
		border-radius: 4rpx;
		background-color: white;
		text-align: center;
		
		&.disabled {
			color: #999;
			border-color: #ddd;
		}
	}
}

// 提交按钮
.submit-btn {
	width: 570rpx;
	height: 80rpx;
	line-height: 80rpx;
	text-align: center;
	font-size: 32rpx;
	margin: 36rpx auto 0;
	color: white;
	background: #ff5033;
	border-radius: 10rpx;
}

// 成功弹窗
.success-modal {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background-color: rgba(0, 0, 0, 0.5);
	z-index: 9999;
	display: flex;
	align-items: center;
	justify-content: center;
	
	.modal-content {
		width: 610rpx;
		background-color: white;
		border-radius: 24rpx;
		padding: 40rpx;
		text-align: center;
		
		.modal-title {
			font-size: 36rpx;
			color: #333;
			margin-bottom: 30rpx;
		}
		
		.modal-text {
			font-size: 28rpx;
			color: #666;
			margin-bottom: 40rpx;
		}
		
		.modal-buttons {
			display: flex;
			justify-content: space-between;
			gap: 20rpx;
			
			.btn-cancel,
			.btn-confirm {
				flex: 1;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				font-size: 30rpx;
				border-radius: 8rpx;
			}
			
			.btn-cancel {
				background-color: #f5f5f5;
				color: #666;
			}
			
			.btn-confirm {
				background-color: #13d151;
				color: white;
			}
		}
	}
}

// 屏幕遮罩
.mask-click {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background-color: rgba(0, 0, 0, 0.5);
	z-index: 9998;
}
</style>